import { getModelForClass, prop } from "@typegoose/typegoose"
import { ApiProperty } from "@nestjs/swagger"
import { IsNotEmpty } from "class-validator"
import { hashSync } from "bcryptjs"
import * as mongoose from 'mongoose';
import { ModelType } from "@typegoose/typegoose/lib/types";

export class User {

    @ApiProperty({ description: "账户", example: "user" })
    @IsNotEmpty({ message: "请填写用户名" })
    @prop({ unique: true })
    public username: string


    @ApiProperty({ description: "密码", example: "123456" })
    @IsNotEmpty({ message: "请填写密码" })
    @prop({
        select: false,
        get(val) {
            return val
        },
        set(val) {
            return val ? hashSync(val) : val
        }
    })
    public password: string

    //创建时间
    @prop()
    public time: number

}


(async () => {
    await mongoose.connect('mongodb://localhost:27017/huoma', { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true });
    const userModel: ModelType<User> = getModelForClass(User);
    try {
        await userModel.create({
            username: 'admin',
            password: 'admin',
            time: Math.floor(Date.now() / 1000)
        } as User)
    } catch (error) { };
})()